package com.pt25.service.sys.repository.mysql.persistence.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pt25.base.dep.db.DbConstants;
import com.pt25.base.util.PageUtil;
import com.pt25.base.util.StrUtil;
import com.pt25.service.sys.domain.admin.ddo.AdminUser;
import com.pt25.service.sys.repository.mysql.converter.AdminConverter;
import com.pt25.service.sys.repository.mysql.mapper.AdminUserMapper;
import com.pt25.service.sys.repository.mysql.persistence.AdminUserRepo;
import com.pt25.service.sys.repository.mysql.po.AdminUserPo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AdminUserRepoImpl extends ServiceImpl<AdminUserMapper, AdminUserPo> implements AdminUserRepo {
    @Override
    public AdminUser getAdminUser(Long id) {
        return AdminConverter.adminUserPO2AdminUser(getById(id));
    }

    @Override
    public AdminUser getLoginAdminUser(AdminUserPo adminUserPO) {
        QueryWrapper<AdminUserPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AdminUserPo::getAdmin, adminUserPO.getAdmin());
        queryWrapper.lambda().eq(AdminUserPo::getPassword, adminUserPO.getPassword());
        queryWrapper.lambda().eq(AdminUserPo::getDelFlag, DbConstants.DEL_FLAG0);
        return AdminConverter.adminUserPO2AdminUser(getOne(queryWrapper));
    }

    @Override
    public AdminUser getAdminUserByAdmin(String admin) {
        QueryWrapper<AdminUserPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AdminUserPo::getAdmin, admin);
        return AdminConverter.adminUserPO2AdminUser(getOne(queryWrapper));
    }

    @Override
    public PageUtil.PageVo pageAdminUser(AdminUserPo adminUserPO) {
        Page<AdminUserPo> page = new Page<>(adminUserPO.getPageNum(), adminUserPO.getPageSize());
        QueryWrapper<AdminUserPo> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(adminUserPO.getAdmin())) {
            queryWrapper.lambda().eq(AdminUserPo::getAdmin, adminUserPO.getAdmin());
        }
        baseMapper.selectPage(page, queryWrapper);
        return PageUtil.PageVo.builder()
                .total(page.getTotal())
                .pageNum(page.getCurrent())
                .pageSize(page.getSize())
                .list(page.getRecords())
                .build();
    }
}
